package com.jyxd.web.service.ZSqlSettings.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyxd.web.dao.ZSqlSettings.ZSqlSettingsDao;
import com.jyxd.web.data.ZSqlSettings.ZSqlSettingsEntity;
import com.jyxd.web.data.ZSqlSettings.form.ZSqlSettingSearchForm;
import com.jyxd.web.data.ZSqlSettings.form.ZZhongZhengSearchForm;
import com.jyxd.web.service.ZSqlSettings.ZSqlSettingsService;
import com.jyxd.web.util.SqlUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZSqlSettingsServiceImpl extends ServiceImpl<ZSqlSettingsDao, ZSqlSettingsEntity> implements ZSqlSettingsService {

    @Resource
    private ZSqlSettingsDao zSqlSettingsDao;
    @Override
    public Page<ZSqlSettingsEntity> getSqlSetting(ZSqlSettingSearchForm zSqlSettingSearchForm) {
        Page<ZSqlSettingsEntity> page = new Page<>(zSqlSettingSearchForm.getStart(), zSqlSettingSearchForm.getPageSize());
        LambdaQueryWrapper<ZSqlSettingsEntity> lqw = new LambdaQueryWrapper<>();
        if (zSqlSettingSearchForm.getMenuType() != null && !zSqlSettingSearchForm.getMenuType().isEmpty()){
            lqw.eq(ZSqlSettingsEntity::getMenuType, zSqlSettingSearchForm.getMenuType());
        }
        // 执行分页查询
        return this.page(page, lqw);
    }

    @Override
    public List<Map<String, Object>> getList(ZZhongZhengSearchForm zZhongZhengSearchForm) {
        LambdaQueryWrapper<ZSqlSettingsEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZSqlSettingsEntity::getMenuType, zZhongZhengSearchForm.getMenuType());
        lqw.eq(ZSqlSettingsEntity::getSqlType, zZhongZhengSearchForm.getSqlType());
        ZSqlSettingsEntity one = this.getOne(lqw);
        if (one == null){
            return null;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", zZhongZhengSearchForm.getStartTime());
        params.put("endDate", zZhongZhengSearchForm.getEndTime());
        String s = SqlUtil.replacePlaceholders(one.getSqlString(), params);
        return zSqlSettingsDao.getList(s);
    }
}
